<?php
/**
 * ceyloan Theme Options
 *
 * @package WordPress
 * @subpackage ceyloan
 * @since ceyloan 1.0
 */

/**
 * Register the form setting for our ceyloan_options array.
 *
 * This function is attached to the admin_init action hook.
 *
 * This call to register_setting() registers a validation callback, ceyloan_theme_options_validate(),
 * which is used when the option is saved, to ensure that our option values are complete, properly
 * formatted, and safe.
 *
 * We also use this function to add our theme option if it doesn't already exist.
 *
 * @since ceyloan 1.0
 */
function ceyloan_theme_options_init() {

	// If we have no options in the database, let's add them now.
	if ( false === ceyloan_get_theme_options() )
		add_option( 'ceyloan_theme_options', ceyloan_get_default_theme_options() );

	register_setting(
		'ceyloan_options',       // Options group, see settings_fields() call in theme_options_render_page()
		'ceyloan_theme_options', // Database option, see ceyloan_get_theme_options()
		'ceyloan_theme_options_validate' // The sanitization callback, see ceyloan_theme_options_validate()
	);
}
add_action( 'admin_init', 'ceyloan_theme_options_init' );

/**
 * Add our theme options page to the admin menu, including some help documentation.
 *
 * This function is attached to the admin_menu action hook.
 *
 * @since ceyloan 1.0
 */
function ceyloan_theme_options_add_page() {
	$theme_page = add_theme_page(
		__( 'Theme Options', 'ceyloan' ),   // Name of page
		__( 'Theme Options', 'ceyloan' ),   // Label in menu
		'edit_theme_options',                    // Capability required
		'theme_options',                         // Menu slug, used to uniquely identify the page
		'ceyloan_theme_options_render_page' // Function that renders the options page
	);

	if ( ! $theme_page )
		return;

	$help = '<p>' . __( 'Some themes provide customization options that are grouped together on a Theme Options screen. If you change themes, options may change or disappear, as they are theme-specific. Your current theme, ceyloan, provides the following Theme Options:', 'ceyloan' ) . '</p>' .
			'<ol>' .
				'<li>' . __( '<strong>Box count</strong>: Number of boxes of the upper left corner excluding logo box.', 'ceyloan' ) . '</li>' .
				'<li>' . __( '<strong>url_X</strong>: URL of the X th box from the left.', 'ceyloan' ) . '</li>' .
				'<li>' . __( '<strong>Background_url_X</strong>: background url of the x th box size of the image is similar to that of logo.png which comes with the theme.', 'ceyloan' ) . '</li>' .
			'</ol>' .
			'<p>' . __( 'Remember to click "Save Changes" to save any changes you have made to the theme options.', 'ceyloan' ) . '</p>' .
			'<p><strong>' . __( 'For more information:', 'ceyloan' ) . '</strong></p>' .
			'<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Theme_Options_Screen" target="_blank">Documentation on Theme Options</a>', 'ceyloan' ) . '</p>' .
			'<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>', 'ceyloan' ) . '</p>';

	add_contextual_help( $theme_page, $help );
}
add_action( 'admin_menu', 'ceyloan_theme_options_add_page' );

/**
 * Returns the default options for ceyloan.
 *
 * @since ceyloan 1.0
 */
function ceyloan_get_default_theme_options() {
	$default_theme_options = array(
		'box-count' => 0,
		'url_1'=>'#',
		'url_2'=>'#',
		'url_3'=>'#',
		'url_4'=>'#',
		'back_1'=>'no.png',
		'back_2'=>'no.png',
		'back_3'=>'no.png',
		'back_4'=>'no.png',
		'bcat_1'=>'',
		'bcat_2'=>'',
		'bcat_3'=>'',
		'so_sh'=>0,
	);

	return apply_filters( 'ceyloan_default_theme_options', $default_theme_options );
}

/**
 * Returns the options array for ceyloan.
 *
 * @since ceyloan 1.0
 */
function ceyloan_get_theme_options() {
	return get_option( 'ceyloan_theme_options', ceyloan_get_default_theme_options() );
}

/**
 * Returns the options array for ceyloan.
 *
 * @since ceyloan 1.2
 */
function ceyloan_theme_options_render_page() {?>
	<div class="wrap">
		<h2><?php printf( __( '%s Theme Options', 'ceyloan' ), get_current_theme() ); ?></h2>
		<?php settings_errors(); ?>
		<form method="post" action="options.php">
			<?php
				settings_fields( 'ceyloan_options' );
				$options = ceyloan_get_theme_options();
				$default_options = ceyloan_get_default_theme_options();
			?>
			<fieldset>
			<label>No of Boxes</label><input type="text" name="ceyloan_theme_options[box-count]" value="<?php echo esc_attr($options['box-count']) ?>" /><br /><br />
			<label>URL_1</label><input type="text" name="ceyloan_theme_options[url_1]" value="<?php echo esc_attr($options['url_1']) ?>" /><br />
			<label>URL_2</label><input type="text" name="ceyloan_theme_options[url_2]" value="<?php echo esc_attr($options['url_2']) ?>" /><br />
			<label>URL_3</label><input type="text" name="ceyloan_theme_options[url_3]" value="<?php echo esc_attr($options['url_3']) ?>" /><br />
			<label>URL_4</label><input type="text" name="ceyloan_theme_options[url_4]" value="<?php echo esc_attr($options['url_4']) ?>" /><br /><br />
			</fieldset>
			<fieldset>
			<label>Background_URL_1</label><input type="text" name="ceyloan_theme_options[back_1]" value="<?php echo esc_attr($options['back_1']) ?>" /><br />
			<label>Background_URL_2</label><input type="text" name="ceyloan_theme_options[back_2]" value="<?php echo esc_attr($options['back_2']) ?>" /><br />
			<label>Background_URL_3</label><input type="text" name="ceyloan_theme_options[back_3]" value="<?php echo esc_attr($options['back_3']) ?>" /><br />
			<label>Background_URL_4</label><input type="text" name="ceyloan_theme_options[back_4]" value="<?php echo esc_attr($options['back_4']) ?>" /><br /><br />
			</fieldset>
			<?php $categories=get_categories(); ?>
			<label>Category of Excerpt_Box_1</label>
			<select name="ceyloan_theme_options[bcat_1]">
			<?php 
				foreach($categories as $category){
					if(get_cat_name(get_option('default_category'))==$category->name)
						continue;
					if($category->name==$options['bcat_1'])
						echo "<option selected='selected' value='".$category->name."' >".$category->name."</option>";
					else
						echo "<option value='".$category->name."' >".$category->name."</option>";
				}
			?>
			</select><br />
			<label>Category of Excerpt_Box_2</label>
			<select name="ceyloan_theme_options[bcat_2]">
			<?php 
				foreach($categories as $category){
					if(get_cat_name(get_option('default_category'))==$category->name)
						continue;
					if($category->name==$options['bcat_2'])
						echo "<option selected='selected' value='".$category->name."' >".$category->name."</option>";
					else
						echo "<option value='".$category->name."' >".$category->name."</option>";
				}
			?>
			</select><br />
			<label>Category of Excerpt_Box_3</label>
			<select name="ceyloan_theme_options[bcat_3]">
			<?php 
				foreach($categories as $category){
					if(get_cat_name(get_option('default_category'))==$category->name)
						continue;
					if($category->name==$options['bcat_3'])
						echo "<option selected='selected' value='".$category->name."' >".$category->name."</option>";
					else
						echo "<option value='".$category->name."' >".$category->name."</option>";
				}
			?>
			</select><br /><br />
			<label>Social Sharing</label><select name="ceyloan_theme_options[so_sh]">
				<option value="0">Off</option>
				<?php if($options['so_sh']==1){
					echo '<option value="1" selected="selected">On</option>';
				} else{
					echo '<option value="1">On</option>';
				}?>
			</select>
			<?php submit_button(); ?>
		</form>
	</div>
<?php
}

/**
 * Sanitize and validate form input. Accepts an array, return a sanitized array.
 *
 * @see ceyloan_theme_options_init()
 * @todo set up Reset Options action
 *
 * @since ceyloan 1.0
 */
function ceyloan_theme_options_validate( $input ) {
	$output = $defaults = ceyloan_get_default_theme_options();
	if(isset($input['box-count']) && $input['box-count']>=0 && $input['box-count']<=4){
		$output['box-count']=$input['box-count'];	
	}
	
	
	if(isset($input['url_1']) && $input['url_1']){
		$output['url_1']=$input['url_1'];
	}
	if(isset($input['url_2']) && $input['url_2']){
		$output['url_2']=$input['url_2'];
	}
	if(isset($input['url_3']) && $input['url_3']){
		$output['url_3']=$input['url_3'];
	}
	if(isset($input['url_4']) && $input['url_4']){
		$output['url_4']=$input['url_4'];
	}
	
	
	if(isset($input['back_1']) && $input['back_1']){
		$output['back_1']=$input['back_1'];
	}
	if(isset($input['back_2']) && $input['back_2']){
		$output['back_2']=$input['back_2'];
	}
	if(isset($input['back_3']) && $input['back_3']){
		$output['back_3']=$input['back_3'];
	}
	if(isset($input['back_4']) && $input['back_4']){
		$output['back_4']=$input['back_4'];
	}
	
	
	if(isset($input['bcat_1'])){
		$output['bcat_1']=$input['bcat_1'];
	}
	if(isset($input['bcat_2'])){
		$output['bcat_2']=$input['bcat_2'];
	}
	if(isset($input['bcat_3'])){
		$output['bcat_3']=$input['bcat_3'];
	}
	if(isset($input['so_sh'])){
		$output['so_sh']=$input['so_sh'];
	}
	return apply_filters( 'ceyloan_theme_options_validate', $output, $input, $defaults );
}
?>
